<template>
  <div id="Login">
    <el-card class="login-form" style="text-align: center; opacity:0.6;">
      <el-row style="margin-top: 100px">
        <el-col :span="12"
          ><el-row :gutter="1" style="margin-top: -50px">
            <h2 style="color: #c3c4c6">大数据学院素拓系统</h2>
          </el-row>
          <el-row :gutter="1">
            <img
              src="../assets/images/icon.png"
              style="width: 120px; height: 120px; margin-top: 20px"
            />
          </el-row>
          <el-row>
            <div align="center" style="margin-top: 20px; color: #c3c4c6">
              <el-link type="info" href="http://www.beianw.com" disabled>
                © 2020 flame 赣ICP备20005291号-1</el-link
              >
            </div>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-form
            ref="userInfoRef"
            :model="userInfo"
            label-width="80px"
            :rules="userRules"
            style="width: 350px; margin-top: -30px"
          >
            <el-form-item
              prop="num"
              :rules="[
                {
                  required: true,
                  message: '学号/工号不能为空',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                type="text"
                placeholder="请输入学号/工号"
                v-model="userInfo.num"
                prefix-icon="iconfont el-icon-user"
              ></el-input>
            </el-form-item>
            <el-form-item
              prop="password"
              :rules="[
                {
                  required: true,
                  message: '密码不能为空！',
                  trigger: 'blur',
                },
              ]"
            >
              <el-input
                placeholder="请输入密码"
                v-model="userInfo.password"
                showPassword
                prefix-icon="el-icon-suitcase-1"
                @keyup.enter.native="userLogin"
              ></el-input>
            </el-form-item>
            <el-form-item
              style="width: 350px"
              prop="type"
              :rules="[
                {
                  required: true,
                  message: '登录类型不能为空！',
                  trigger: 'blur',
                },
              ]"
            >
              <el-select
                v-model="userInfo.type"
                placeholder="请选择登录类型"
                style="width: 270px"
              >
                <el-option label="学生" value="true"> </el-option>
                <el-option label="教师" value="false"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                type="success"
                round
                style="width: 45%"
                @click="userLogin"
                class="login-button"
                :loading="loading"
                @keyup.enter.native="userLogin"
                >登录</el-button
              >
              <el-button
                type="primary"
                round
                style="width: 45%"
                class="login-button"
                @click="reset"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script>
import { login } from "api/api";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      userInfo: { num: "", password: "" },
      loading: false,
      userRules: {},
    };
  },
  methods: {
    ...mapMutations(["setHeader"]),
    //用户登录
    userLogin() {
      this.loading = true;
      this.$refs.userInfoRef.validate((valid) => {
        if (!valid) {
          this.loading = false;
          return;
        } else {
          login(this.userInfo)
            .then((res) => {
              if (!res.flag) {
                this.$message.error(res.message);
                this.loading = false;
              } else {
                //将用户信息放到vuex中
                this.setHeader(res.data);
                window.sessionStorage.setItem(
                  "token",
                  JSON.stringify(res.data)
                );
                this.loading = false;
                this.$router.push({ path: "/home" });
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("请求超时，请重试！");
            });
        }
      });
    },
    reset() {
      this.userInfo = {};
    },
  },
};
</script>
<style scoped>
.login-form {
  position: absolute;
  left: 0;
  right: 0;
  width: 860px;
  height: 400px;
  margin: 200px auto;
}
.login-button {
  position: relative;
  top: 20px;
}
</style>